<?PHP

class UserBO extends BusinessObject{
  /* private function using in constructors*/
  function setAttributes(){
    $this->fields   = array(
			"id"                =>	"users_id", 
			"login"             =>	"users_username",
			"password"          =>	"users_password",
			"email"             =>	"users_email",
			"fname"		          =>	"users_firstname",
			"lname"		          =>	"users_lastname",
			"country"           =>  "users_country",
			"state"             =>  "users_state",
			"city"              =>  "users_city",
			"zip"               =>  "users_zip",
			"address"           =>  "users_address",
			"address2"          =>  "users_address2",
			"phone"             =>	"users_phone",
			"confirmed"         =>	"users_confirmed",
			"confirmation_code" =>	"users_confirm_code",
			"last_login"        =>	"users_date_login",
			"deleted"           =>	"users_deleted", 
			"disabled"          =>	"users_disabled",
			"reg_date"	        =>	"users_date_register",
			"secureString"	    =>	"users_secureString",
			"tempPasswd"	      =>	"users_tempPasswd",
			"site"		          =>	"user_site",
			"CCNumber"	        =>	"users_CCNumber",
			"CCNameOnCard"	    =>	"users_CCNameOnCard",
			"CCType"	          =>	"users_CCType",
			"CCExpireMonth"     =>	"users_CCExpireMonth",
			"CCExpireYear"	    =>	"users_CCExpireYear",
			"CVV2"              =>  "users_CVV2",
			"agn_agent"         =>  "users_agn_agent",
			"inhome"			=>	"users_agn_inhome",
			"agn_agency"        =>  "users_agn_agency",
			"agn_agencytype"    =>  "users_agn_agencytype",
			"agn_IATA"          =>  "users_agn_IATA",
			"agn_website"       =>  "users_agn_website",
			"agn_discountrate"  =>  "users_agn_discountrate",
			"agn_prepaid"       =>  "users_agn_prepaid",
			"agn_needagentid"   =>  "users_agn_needagentid",
    		"agn_region" 	    =>  "users_agn_region",
    		'has_ccreader'      =>  "users_agn_HasCCReader"
		);

//UPDATE `users` SET `users_name_on_voucher` = concat( `users_firstname` , ' ', `users_lastname` ) 

    BusinessObject::setAttributes();
  }
  /* constuctor */
  function UserBO(){
    /** super(tableName) */
    BusinessObject::BusinessObject("users");
    $this->setAttributes();
  }

  /*  function checkLogin  
      if username exist and not this user's then return false else true.
  */
  function checkLogin(){
    $u = new UserBO();
    $u->login = $this->login;
	$u->deleted = 0;
    $u->Load(0,1);
    if($u->Next()){
      if($u->id != $this->id)
        return(false);
    }
    return(true);
  }
  /*  function checkEmail  
      if email already exist and not this user's then return false else true.
  */
  function checkEmail(){
    $u = new UserBO();
    $u->email = $this->email;
	$u->deleted = 0;
    $u->Load(0,1);
    if($u->Next()){
      if($u->id != $this->id)
        return(false);
    }
    return(true);
  }

  /*  Save  */
  function Save($userID){
    if(is_numeric($userID) && $userID<>0){
      $this->id = "$userID";
      if(!($this->checkLogin())) return (" User with Login '{$this->login}' already exist");
      if(!($this->checkEmail())) return (" User with Email '{$this->email}' already exist");
      $this->Update("{$this->fields[id]} = '$userID'");  
    } else{
      if(!($this->checkLogin())) return (" User with Login '{$this->login}' already exist");
      if(!($this->checkEmail())) return (" User with Email '{$this->email}' already exist");
      $this->Insert();
      $this->id = $this->getInsertID();
    }
  }

  function setLastLogin()
  {
    OB_START();
     $this->last_login = date("Y-m-d H:i:s");
     $this->Update($this->fields["id"]."=".$this->id);
    OB_END_CLEAN();
  }
}
?>
